package com.example.lovebond.controller;

import com.example.lovebond.common.Const;
import com.example.lovebond.common.HttpResult;
import com.example.lovebond.dao.mapper.generatedMapper.BondMapper;
import com.example.lovebond.dao.mapper.generatedMapper.BondTemplateMapper;
import com.example.lovebond.dao.mapper.generatedMapper.LoveBondUserMapper;
import com.example.lovebond.dao.model.Bond;
import com.example.lovebond.dao.model.BondTemplate;
import com.example.lovebond.dao.model.BondTemplateExample;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@RestController
@RequestMapping("/bond")
public class BondController {

    private final LoveBondUserMapper loveBondUserMapper;
    private final BondTemplateMapper bondTemplateMapper;
    private final BondMapper bondMapper;

    public BondController(BondTemplateMapper bondTemplateMapper, BondMapper bondMapper, LoveBondUserMapper loveBondUserMapper) {
        this.loveBondUserMapper = loveBondUserMapper;
        this.bondTemplateMapper = bondTemplateMapper;
        this.bondMapper = bondMapper;
    }

    @GetMapping("/list")
    public HttpResult selectBondList() {
        var example = new BondTemplateExample();
        example.createCriteria();
        var bondList = this.bondTemplateMapper.selectByExample(example);
        return HttpResult.ok(bondList);
    }

    @PostMapping("/use")
    public HttpResult useBondById(@RequestBody Map<String, String> param) {
        var bondId = param.get("bondId");
        var bond = this.bondMapper.selectByPrimaryKey(bondId);
        if (bond.getBondState() != Const.BOND_STATE.ALLOWED) return HttpResult.error("好像还没商量好哦～");
        else {
            var updatedBond = new Bond();
            updatedBond.setBondId(bondId);
            updatedBond.setBondState(Const.BOND_STATE.USED);
            updatedBond.setUpdateTime(new Date());
            this.bondMapper.updateByPrimaryKeySelective(updatedBond);
            return HttpResult.ok();
        }
    }

    @PostMapping("/allowUse")
    public HttpResult allowUse(@RequestBody Map<String, String> param) {
        var bondId = param.get("bondId");
        var bond = this.bondMapper.selectByPrimaryKey(bondId);
        var state = bond.getBondState();
        if (state == Const.BOND_STATE.DEFAULT) state = Const.BOND_STATE.ALLOWED;
        else if (state == Const.BOND_STATE.ALLOWED) state = Const.BOND_STATE.DEFAULT;
        var updatedBond = new Bond();
        updatedBond.setBondId(bondId);
        updatedBond.setBondState(state);
        updatedBond.setUpdateTime(new Date());
        this.bondMapper.updateByPrimaryKeySelective(updatedBond);
        return HttpResult.ok();
    }

    @PostMapping("/add")
    public HttpResult addBond(@RequestBody Map<String, String> param) {
        var userId = param.get("userId");
        var bondReason = param.get("bondReason");
        var templateId = param.get("templateId");
        var bondTemplate = this.bondTemplateMapper.selectByPrimaryKey(templateId);
        var user = this.loveBondUserMapper.selectByPrimaryKey(userId);

        var bond = new Bond();
        bond.setBondName(bondTemplate.getTemplateName());
        bond.setBondDesc(bondTemplate.getTemplateDesc());
        bond.setBondReason(bondReason);
        bond.setOwnerId(user.getMateId());
        bond.setIssuerId(userId);
        bond.setBondState(1);
        bond.setCreateTime(new Date());
        bond.setUpdateTime(new Date());
        this.bondMapper.insertSelective(bond);
        return HttpResult.ok();
    }
}
